<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" :visible.sync="visible" width="60%" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table v-loading="loading" @row-click="clickRow" ref="table" :data="tableData" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" :selectable="selectable"></el-table-column>
        <el-table-column label="登录账号" prop="account" :show-overflow-tooltip="true" />
        <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="部门" prop="orgName" :show-overflow-tooltip="true" />
        <el-table-column label="手机" prop="mobile" :show-overflow-tooltip="true" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPersonnel } from "../../../api/leaveApplication";
export default {
  dicts: ['sys_normal_disable'],
  props: {
    // 角色编号
    selectType: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      userIds: [],
      names: [],
      accounts: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        orgName: undefined,
        showRole: 1
      },
      loading: true
    };
  },
	computed: {
		tableData: function () {
			if(this.queryParams.name){
        return this.userList.filter(e=>e.name.includes(this.queryParams.name))
      }else if(this.queryParams.mobile){
        return this.userList.filter(e=>e.mobile.includes(this.queryParams.mobile))
      }else if(this.queryParams.orgName){
        return this.userList.filter(e=>e.orgName.includes(this.queryParams.orgName))
      }else{
        return this.userList
      }
		},
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        showRole: 1
      }
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.personId);
      this.names = selection.map(item => item.name);
      this.accounts = selection.map(item => item.account);
    },
    // 查询表数据
    getList() {
      this.loading = true
      getPersonnel(this.queryParams).then(res => {
        let list = []
        this.userList = res.data.forEach(e=>{
          list.push(...e.cimContactsVOList)
        });
        this.userList = [...list]
        this.loading = false
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权用户操作 */
    handleSelectUser() {
      const ids = this.userIds.join(",");
      const names = this.names.join(",");
      const accounts = this.accounts.join(",");
      if (ids == "") {
        this.$modal.msgError("请选择人员");
        return;
      }
      this.$emit('getUserInfo', { ids, names, accounts }, this.selectType)
      this.visible = false
    },
    selectable(row, index){
      if(this.selectType=='leader'){
        if(this.userIds.length>0&&this.userIds.every(e=>e!=row.personId)){
          return false
        }else{
          return true
        }
      }else{
        return true
      }
    }
  }
};
</script>
